<?php
/**
 * 
 * @author Freezy
 *
 */
class Dashboard extends MY_Controller {

	function Dashboard(){
		parent::MY_Controller();
		$this->validate_cookie();
	}
	
	function index(){
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($this->session->userdata('usrname'));

		$this->load->model(array('departemen_model','jabatan_model',
			'karyawan_model','karyawan_history_jabatan_model'));
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array(
			'view'=>'template/horz_nav', 
			'view_data'=>array(
				'data'=>$CONF['nav']['psdm']
			) 
		);
		
		$this->load->model('karyawan_history_jabatan_model');
		$layoutConf['right_content'] = array('view'=>'dashboard_psdm', 
			'view_data'=>array(
				'limit' => 5,//tampilkan sedikit saja$CONF['search']['limit'],
				'usrAlias'=>$usrAlias,
				'usrName'=>$usrName,
				'historyJabatan'=>$this->karyawan_history_jabatan_model->getActive($usrName)
			));
		$this->initialize_layout($layoutConf);
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript_default'][] = 'psdm/dashboard';
		$pageConf['jscript'] = $pageConf['jscript_default'];
		$pageConf['css'] = $pageConf['css_default'];
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();
	}
	
	function _search(){
		$this->load->model('departemen_model');

		$key = $this->input->post('key');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		
		$table_content = "";
		$row = 0;
		$data = $this->departemen_model->select_last_entry($key, $limit, $offset, $orderby);
		$table_content = $this->load->view('departemen/table_content', array('dep_data'=>$data),true); 	
		echo json_encode(array(
			'table_content'=>$table_content,
			'table_num_record'=> $key? $data->num_rows() : $this->departemen_model->count_record()
		));
	}
	
	function _addModal(){
		$this->load->helper(array('html','url'));
		$this->load->view('departemen/add');
	}
	
	function _save(){
		$this->load->model('departemen_model');
		$inp = $this->input->post('inp');
		
		//save
		$res = $this->departemen_model->insert( array('dep_id' => $inp[0] ,'dep_nama' => $inp[1]) );
		$msg = $res? "Proses simpan selsai" : "Proses simpan gagal, ".$this->departemen_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _editModal($dep_id){
		$this->load->model('departemen_model');
		$result = $this->departemen_model->select_by_id($dep_id);
		$result = $result->row();
		$arr = array(
			'dep_id'=>$result->dep_id,
			'dep_nama'=>$result->dep_nama
		);
		$this->load->view('departemen/edit', $arr);
	}
	
	function _update(){
		$this->load->model('departemen_model');
		$inp = $this->input->post('inp');
		
		//save
		$res = $this->departemen_model->update(array('dep_id' => $inp[0]), array('dep_nama' => $inp[1]) );
		$msg = $res? "Proses update selesai" : "Proses update gagal, ".$this->departemen_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _delete($dep_id){
		$this->load->model('departemen_model');
		$res = $this->departemen_model->delete(array('dep_id' => $dep_id));
		$msg = $res? "Proses hapus selesai" : "Proses hapus gagal, ".$this->departemen_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
}
/* End of file psdm/departemen.php */
/* Location: ./system/application/controllers/psdm/departemen.php */